<?php

class lib_weixin extends lib_base{
	public $table_name = "";
	public $pk = "";

	//微信登录
	public function wxlogin($code,$config)
    {
        // 验证appid和appsecret是否填写
        if (empty($config['app_id']) || empty($config['app_secret'])) {
            $this->error = '请填写appid 和 appsecret';
            return false;
        }
        // 微信登录 (获取session_key)
        $WxUser = new lib\Weixin($config);
        $session = $WxUser->login($code);
        return $session;
	}

	//内容审查
	public function checkText($config,$content)
    {
        $this->config = $config;
        $WxUser = new lib\Weixin($this->config);
        $result = $WxUser->checkText($content);
        return $result;
    }

	//生成二维码
    public function getQrcode($config,$data)
    {
        $this->config = $config;
        $WxUser = new lib\Weixin($this->config);
        $result = $WxUser->getQrcode($data);
        return $result;
    }

	//获取手机号
    public function getUserTel($config,$code,$iv,$encryptedData){
        $this->config = $config;
        $WxUser = new lib\Weixin($this->config);
        $result = $WxUser->getTel($code,$iv,$encryptedData);
        return $result;
	}

	//微信支付
	public function payment($config,$order){
		$WxUser = new lib\Weixin($config);
		$result = $WxUser->payment($config,$order);
        return $result;
    }

    // 模版消息
    function sendTemplateMessage($accessToken, $openId, $templateId, $data, $url='') {

        $postData = array(
            'touser' => $openId,
            'template_id' => $templateId,
            'url' => $url,
            'data' => $data
        );

        // 发送模板消息
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$accessToken}";
        $options = [
            'http' => [
                'header'  => "Content-type: application/json\r\n",
                'method'  => 'POST',
                'content' => json_encode($postData)
            ]
        ];
        $context  = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
        // echo $result;

        return $result;

        // $jsonData = json_encode($postData);

        // $ch = curl_init();
        // curl_setopt($ch, CURLOPT_URL, $apiUrl);
        // curl_setopt($ch, CURLOPT_POST, true);
        // curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        // $response = curl_exec($ch);
        // curl_close($ch);

        // // Handle response
        // return $response;
    }

    function sendWxMessage($data,$url='') {
        // 要发送的模板消息的数据
        $templateData = array(
            'character_string1' => array('value' => $data['first']),
            'thing8' => array('value' => $data['keyword1']),
            'time6' => array('value' => $data['keyword2']),
            // 'remark' => array('value' => $data['remark'])
        );

        $response = $this->sendTemplateMessage($data['accessToken'], $data['openId'], $data['templateId'], $templateData,$url);

        // // 处理发送结果
        $result = json_decode($response, true);
        if($result['errcode'] == 0) {
            // echo "模板消息发送成功！";
            return true;
        } else {
            // echo "模板消息发送失败，请稍后重试。错误信息：" . $result['errmsg'];
            // dump($result);
            // 43004没有关注
            return false;
        }
    }

}